<?php
/**
 * @Author: liuhongbin
 * @Email: 514660094@qq.com
 * @Date: 2019-02-12 16:15:10
 * @Description:
 * @LastEditors: liuhongbin
 * @LastEditTime: xxx
 */

namespace app\controller\adminapi;

use app\common\controller\AdminController;
use chillerlan\QRCode\QRCode;
use think\facade\Cache;
use think\facade\Filesystem;
use think\facade\Log;
use think\facade\Route;

/**
 * Class Common 公共控制器
 * @package app\controller\adminapi
 * User: liuhongbin
 * Date: 2021-06-23 12:35
 */
class Common extends AdminController
{

    protected $notLogin = ['qr', 'getPluginJs','getIcons'];

    /**
     * 上传
     */
    public function upload()
    {
        $disks = array('local', 'public', 'cert');
        $path = input('path', 'other');//上传目录
        $disk = input('disk', 'local'); //上传位置
        if (!in_array($disk, $disks)) {
            $this->error('上传位置错误,检查disk参数是否为local，public，cert');
        }
        $file = $this->request->file('file');
        $saveName = Filesystem::disk($disk)->putFile($path, $file);
        return $this->success('上传成功', $saveName);
    }

    public function getLink($plaid = 0)
    {
        $addon_name = get_addon();
        $event_url = event($addon_name . "_addon_url", [])[0] ?? [];
        return $this->success("ok", $event_url);
    }

    /**
     * 获取地址
     */
    public function getLinkInput($plaid = 0)
    {
        function c_url($title, $url, $arr = [])
        {
            return array_merge(['title' => $title, 'url' => $url], $arr);
        }

        //系统地址
        $url = array(
            array(
                'group_name' => "系统链接",
                'children'   => array(
                    [
                        'title'    => '常用地址',
                        'children' => [
                            c_url("返回上一页", 'javascript :history.back(-1)'),
                        ]
                    ],
                    [
                        'title'    => '系统内置(测试版)',
                        'children' => [
                            c_url("个人中心", url("/h5/user", ['p' => $plaid])->build()),
                            c_url("用户钱包", url("/h5/wallet", ['p' => $plaid])->build()),
                            c_url("用户卡券", url("/h5/coupon", ['p' => $plaid])->build()),
                        ]
                    ]
                )
            )
        );
        $event_url = event("system_create_url", []);
        return $this->success("ok", ['system' => $url, 'addons' => $event_url]);
    }

    public function getIcons($query, $limit = 100)
    {
        $api = "https://api.iconify.design/search?query=" . $query;
        $res = file_get_contents($api);
        $res = json_decode($res, true);
        return $this->success("ok", $res['icons']);
    }

    /**
     * 获取二维码
     */
    public function qr()
    {
        $data = input('data', '');
        $size = input('size', 5);
        $margin = input('margin', 2);
        $level = input('level', 'H');
        switch ($level) {
            case 'L':
                $level = QRCode::ECC_L;
                break;
            case 'M':
                $level = QRCode::ECC_M;
                break;
            case 'Q':
                $level = QRCode::ECC_Q;
                break;
            case 'H':
                $level = QRCode::ECC_H;
                break;
            default:
                $level = QRCode::ECC_H;
        }
        return make_code($data, $size, $margin, $level);
    }

    /**
     * 清理
     * @param string $type
     * @param string $key
     */
    public function clear($type = 'cache')
    {
        if ($type == 'cache') {
            Cache::clear();
        }
        return $this->success("清理成功");
    }

    /**
     * 获取页面的hook
     */
    public function getTemplateHooks()
    {
        $hooks = input('hooks', []);
        $dkSys = input('dkSys', []);
        $all_hooks = array(
            'header_user_right_hook',
            'header_user_left_hook'
        );
        $hooks = array_intersect($hooks, $all_hooks);
        $data = [];
        foreach ($hooks as $key => $value) {
            $event = event($value, $dkSys);
            $data[$value] = '';
            foreach ($event as $k => $val) {
                $data[$value] .= $val;
            }
        }
        return $this->success('', $data);
    }

    public function getCustomerMenu()
    {
        $customerMenu = event('customer_menu_hook');
        $data = [];
        foreach ($customerMenu as $key => $value) {
            $data = array_merge($data, $value);
        }
        return $this->success('', $data);
    }

    public function getPluginJs()
    {
        $dkSys = input('dkSys', []);
        $js = event('insert_addon_js', $dkSys);
        $data = [];
        foreach ($js as $key => $value) {
            $data = array_merge($data, $value);
        }
        return $this->success('', $data);
    }
}
